package com.farm.device.service.impl;

import com.farm.device.dao.DeviceInfoMapper;
import com.farm.device.pojo.DeviceInfo;
import com.farm.device.service.DeviceInfoService;
import com.farm.entity.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeviceInfoServiceImpl implements DeviceInfoService {

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Override
    public List<DeviceInfo> findByDid(Integer did) {

        Example example=new Example(DeviceInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (did!=null){
            criteria.andEqualTo("did",did);
        }
        List <DeviceInfo> deviceInfoList = deviceInfoMapper.selectByExample(example);
        return deviceInfoList;
    }

    @Override
    public void add(DeviceInfo deviceInfo) {
        deviceInfoMapper.insert(deviceInfo);
    }

    @Override
    public void delete(Integer did) {

        List <DeviceInfo> infos = findByDid(did);
        if (infos!=null||infos.size()>0){
            for (DeviceInfo info : infos) {
                Integer id = info.getId();
                deviceInfoMapper.deleteByPrimaryKey(id);
            }

        }
    }

    @Override
    public void update(DeviceInfo deviceInfo) {

        int i = deviceInfoMapper.updateByPrimaryKey(deviceInfo);
        if (i==0){
            throw new RuntimeException("修改失败");
        }


//        Example example = new Example(DeviceInfo.class);
//        Example.Criteria criteria = example.createCriteria();
//        if (example!=null) {
//            criteria.andEqualTo("did", deviceInfo.getDid());
//        }
//        deviceInfoMapper.updateByExampleSelective(deviceInfo,example);
    }

}
